package com.hzdesk.media.koreabatchmatvector.request;

import android.content.Context;
import android.util.Log;


import com.hzdesk.media.koreabatchmatvector.bean.UploadBean;
import com.hzdesk.media.koreabatchmatvector.bean.UploadFileTokenBean;
import com.hzdesk.media.koreabatchmatvector.inter.ICallBack;
import com.hzdesk.media.koreabatchmatvector.utils.JsonUitl;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import okhttp3.FormBody;

/**
 * Created by Administrator on 2018/5/25.
 */

public class UploadFileRequest {
    private static final String TAG = UploadFileRequest.class.getSimpleName();
    private Model model = Model.getInstance();
    private Context context;
    private String Url;

    public UploadFileRequest(Context context, String url) {
        this.context = context;
        Url = url;
    }

    public interface OnUploadFileListener {
        void success(UploadFileTokenBean uploadFileTokenBean);

        void fail(String s);
    }

    private OnUploadFileListener onUploadFileListener;
    private String Success = "SUCCESS";
    private String Fail = "FAIL";

    public void setOnUploadFileListener(OnUploadFileListener onUploadFileListener) {
        this.onUploadFileListener = onUploadFileListener;
    }

    public void setRequestBody(UploadBean body) {
        postRequest(Url, body.getFormBody());
    }


    private void postRequest(String url, Map<String,String> map) {
        model.postMap(context, url, map, new ICallBack() {
            @Override
            public void result(String s) {
                try {
                    JSONObject jsonObject = new JSONObject(s);
                    Log.d(TAG,"get:"+s);
                    if (jsonObject.has("message")) {
                        if (jsonObject.getString("message").equals(Success)) {
                            onUploadFileListener.success((UploadFileTokenBean) JsonUitl.stringToObject(s,UploadFileTokenBean.class));
                        }
                        if (jsonObject.getString("message").equals(Fail)) {
                            onUploadFileListener.fail(s);
                        }
                    }
                } catch (JSONException e) {
                    //e.printStackTrace();
                    Log.d(TAG,s);
                }

            }
        });






    }
}
